<?php

use think\facade\Cache;
use think\facade\Db;
use Webman\Event\Event;
use support\Container;
use Webman\Http\Response;
use support\exception\ValidateException;



use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

//通寻千义 创建角色
function create_character($userInfo)
{

  $appkey = "lm-FtBeFkLYe+FpBJmFL0rkwA==";
  $client = new Client();
  $headers = [
      'Content-Type' => 'application/json',
      'Expect' => '',
      'Accept' => '*/*',
      'x-fag-servicename' => 'aca-character-create',
      'Authorization' => 'Bearer '.$appkey,
      'x-fag-appcode' => 'aca'
  ];

  $basicInformation = str_replace('<br/>', "\n", $userInfo["basicInformation"]);
  $basicInformation = str_replace('<p>', "", $basicInformation);
  $basicInformation = str_replace('</p>', "", $basicInformation);

  $data = [
    "name" => $userInfo["name"],
    "avatar" => [
        "fileUrl" => "https://static.jvlihd.cn/img/userHeader.jpg"
    ],
    "introduction" => "",
    "basicInformation" => $basicInformation,
    "openingLine" => $userInfo["openingLine"],
    "traits" => $userInfo["traits"],
    "chatExample" => "",
    "type" => "virtual",
    "permConfig" => [
        "isPublic" => 0,
        "allowApi" => 0,
        "allowChat" => 0
    ],
    "advancedConfig" => [
        "isRealInfo" => true,
        "searchKeyword" => $userInfo["name"],
        "allowSendImage" => false,
        "allowSendAsr" => false,
        "isRealTime" => true,
        "shortTermMemoryRound" => 20,
        "longTermMemories" => [],
        "platformPlugins" => []
    ]
  ];
  $url = 'http://nlp.aliyuncs.com/v2/api/character/create';

  // var_dump($headers);
  // var_dump($data);

  try {
      $response = $client->post($url, [
          'headers' => $headers,
          'body' => json_encode($data)
      ]);
      $body = $response->getBody();
      $contents = $body->getContents();
      return $contents;

  } catch (RequestException $e) {
      echo "HTTP Request failed\n";
      echo $e->getMessage();
      return false;
  }



}


//通寻千义 删除角色
function delete_character($characterId)
{
 
  // 创建 Guzzle HTTP 客户端实例
  $client = new Client([
    'base_uri' => 'http://nlp.aliyuncs.com/v2/api/character/delete',
    'timeout'  => 5.0,
  ]);

  // 设置请求的头部和数据体
  $headers = [
    'Content-Type' => 'application/json',
    'Expect' => '',
    'Accept' => '*/*',
    'x-fag-servicename' => 'aca-character-delete',
    'x-fag-appcode' => 'aca',
    'Authorization' => 'Bearer lm-FtBeFkLYe+FpBJmFL0rkwA==' // 确保使用正确的 API Key
  ];

  // 请求的数据体
  $data = [
    'characterId' => $characterId,
    'version' => 1
  ];

  try {
    // 发送 POST 请求
    $response = $client->post('character/delete', [
        'headers' => $headers,
        'json' => $data  // 使用 json 会自动设置 Content-Type 为 application/json 并且编码数据
    ]);

    // 获取并打印响应体
    $body = $response->getBody()->getContents();
    return $body;

  } catch (RequestException $e) {
    // 请求失败时，捕获异常
    echo "HTTP Request failed\n";
    echo $e->getMessage();
    if ($e->hasResponse()) {
        // 打印出具体的失败响应
        echo $e->getResponse()->getBody()->getContents();
    }
  }


}

//通寻千义 更新角色
function update_character($userInfo)
{

  // 创建 Guzzle HTTP 客户端实例
  $client = new Client([
    'base_uri' => 'http://nlp.aliyuncs.com/v2/api/character/update',
    'timeout'  => 5.0,
  ]);

  // 设置请求的头部和数据体
  $headers = [
    'Content-Type' => 'application/json',
    'Expect' => '',
    'Accept' => '*/*',
    'x-fag-servicename' => 'aca-character-update',
    'x-fag-appcode' => 'aca',
    'Authorization' => 'Bearer lm-FtBeFkLYe+FpBJmFL0rkwA==' // 确保使用正确的 API Key
  ];

  $basicInformation = str_replace('<br/>', "\n", $userInfo["basicInformation"]);
  $basicInformation = str_replace('<p>', "", $basicInformation);
  $basicInformation = str_replace('</p>', "", $basicInformation);
  // 请求的数据体
  $data = [
    "characterId" => $userInfo["role_id"],
    "name" => $userInfo["name"],
    "avatar" => [
        "fileUrl" => "https://static.jvlihd.cn/img/userHeader.jpg"
    ],
    "basicInformation" =>  $basicInformation,
    "openingLine" => $userInfo["openingLine"],
    "traits" =>  $userInfo["traits"],
  ];

  try {
    // 发送 POST 请求
    $response = $client->post('character/update', [
        'headers' => $headers,
        'json' => $data  // 使用 json 会自动设置 Content-Type 为 application/json 并且编码数据
    ]);

    // 获取并打印响应体
    $body = $response->getBody()->getContents();
    return $body;
  } catch (RequestException $e) {
    // 请求失败时，捕获异常
    echo "HTTP Request failed\n";
    echo $e->getMessage();
    if ($e->hasResponse()) {
        // 打印出具体的失败响应
        echo $e->getResponse()->getBody()->getContents();
    }
  }



}








?>